#include <stdio.h>
#include "somb.h"

void test_method_service_provider(uint32_t handle,uint32_t ServiceId,void*MthReqData,uint32_t MthReqDataLen)
{
    printf("test2 recv ServiceId=%08x handle=%08x datalen=%d\n",ServiceId,handle,MthReqDataLen);
}
int32_t test_event_on_listen_cb(uint32_t ServiceId,void**EvtData,uint32_t *EvtDataLen)
{
    /*if this function is called, that means some other consumer was listenning to BASICCOM_SID_EVENT_TEST*/
    printf("test2 recv Event ServiceId=0x%08x was being listened \n", ServiceId);
    
    /* prepare the memory space for the event data */
    *EvtData = SOMB_ALLOC(32);
    if(NULL == *EvtData)
    {
        printf("### malloc failed\n");
        return SOMB_FAIL;
    }
    memset(*EvtData, 0, 32);

    /* prepare the event data */
    *EvtDataLen = sprintf((char *)*EvtData, "%08x is ready!", ServiceId);

    /*don't free the memory allocated by EvtData if return SOMB_OK, it will auto freed by somb*/
    return SOMB_OK;
}

int32_t deploy_service(void)
{
    void**Instance = NULL;
    Instance = vSOMB_Service_Instance_New();
    if(NULL == Instance)
    {
        printf("new service instance failed");
        return -1;
    }


    int ret = 0;
    uint8_t service_functon_id = 0x01;
    uint8_t service_sub_functon_id = 0x01;
    uint32_t service_id = 0;

    for(int loop=0; loop < 512; loop++)
    {
        if(loop <= 63)
        {
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop;
        }
        else if(loop <= 127)
        {
            service_functon_id = 0x01;
            service_sub_functon_id = 0x02;
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop;
        }
        else if(loop <= 255)
        {
            service_functon_id = 0x02;
            service_sub_functon_id = 0x01; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 
        else if(loop <= 383)
        {
            service_functon_id = 0x02;
            service_sub_functon_id = 0x02; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 
        else 
        {
            service_functon_id = 0x03;
            service_sub_functon_id = 0x03; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 


        if (loop%2 == 0) 
        { 
            ret = i32SOMB_Service_Add(Instance, service_id, SOMB_SVC_TYPE_METHOD,test_method_service_provider); 
        } 
        else 
        { 
            ret = i32SOMB_Service_Add(Instance, service_id, SOMB_SVC_TYPE_EVENT,test_event_on_listen_cb); 
        }

        if(SOMB_OK != ret)
        {
            printf("somb add service(%08x) failed\n", loop);
        }
    }

    ret = i32SOMB_Service_Instance_Deploy(Instance);
    if(SOMB_OK != ret)
    {
        printf("test2 server somb service deploy failed\n");
        return -1;
    }
    else 
    {
        printf("test2 server somb service deploy success\n");
    }


    ret = i32SOMB_Service_Instance_Destroy(Instance);
    if(SOMB_OK != ret)
    {
        printf("somb service destory failed\n");
        return -1;
    }

    return 0;
}

void disable_servive(bool flag)
{
    int32_t ret = 0;
    uint32_t ServiceIdList[512]={0};
    uint8_t service_functon_id = 0x01;
    uint8_t service_sub_functon_id = 0x01;
    uint32_t service_id = 0;
    for(int loop=0; loop < 512; loop++)
    {
        if(loop <= 63)
        {
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop;
        }
        else if(loop <= 127)
        {
            service_functon_id = 0x01;
            service_sub_functon_id = 0x02;
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop;
        }
        else if(loop <= 255)
        {
            service_functon_id = 0x02;
            service_sub_functon_id = 0x01; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 
        else if(loop <= 383)
        {
            service_functon_id = 0x02;
            service_sub_functon_id = 0x02; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 
        else 
        {
            service_functon_id = 0x03;
            service_sub_functon_id = 0x03; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 

        ServiceIdList[loop] = service_id;
    }
    ret = i32SOMB_Service_SetAvailable(ServiceIdList, 512, flag);
    if(SOMB_OK != ret)
    {
        printf("disable service with service id failed\n");
    }
    else
    {
        printf("disable service with service id success\n");
    }

}
void disable_servive_with_mask(bool flag)
{
    printf("input mask:\n");
    uint32_t mask;
    scanf("%08x",&mask);

    printf("input mask_value:\n");
    uint32_t mask_value;
    scanf("%08x",&mask_value);

    /* for example 
     *  mask        = 0xffff0000 or 0xff000000
     *  mask_value  = 0x01010000 or 0x01000000
     */
    int32_t ret = i32SOMB_Service_SetAvailableWithMask(mask, mask_value, flag);

    printf("mask=0x%08x, mask_value=0x%08x to %s\n", mask, mask_value, flag?"enable":"disable");
    if(SOMB_OK != ret)
    {
        printf("disable service with mask failed\n");
    }
    else
    {
        printf("disable service with mask success\n");
    }

}

void print_usage()
{
    printf("1:deploy service\n");
    printf("2:disable service with service id\n");
    printf("3:enable service with service id\n");
    printf("4:disable service with mask\n");
    printf("5:enable service with mask\n");
}
int main(void)
{
    uint32_t SubNsId;
    int32_t ret = i32SOMB_SubNs_Init(&SubNsId);
    if(SOMB_OK != ret)
    {
        printf("%s somb sub ns init failed\n",__FUNCTION__ );
        return -1;
    }
    printf("SubNs=%d SOMB Init success\n",SubNsId);


    while(1)
    {
        print_usage();
        printf("chose your test index:\n");
        int cmd;
        scanf("%d",&cmd);
        printf("your chose is %d \n",cmd);
        switch(cmd)
        {
            case 1: /* deploy service */
                deploy_service();
                break;
            case 2: /* disable service */
                disable_servive(false);
                break;
            case 3: /* enable service */
                disable_servive(true);
                break;
            case 4: /* disable service with mask*/
                disable_servive_with_mask(false);
                break;
            case 5: /* enable service with mask*/
                disable_servive_with_mask(true);
                break;
        }
    }

    return 0;
}
